package com.softwinner.awbt.controller;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;

import com.goodocom.gocsdk.IGocsdkCallback;
import com.goodocom.gocsdk.IGocsdkService;
import com.softwinner.awbt.activity.CallActivity;
import com.softwinner.awbt.activity.InComingActivity;
import com.softwinner.awbt.common.BTApplication;
import com.softwinner.awbt.listener.CallListener;
import com.softwinner.awbt.service.GocsdkCallbackImp;
import com.softwinner.awbt.service.GocsdkService;
import com.softwinner.awbt.utils.LOG;

import java.util.List;

/**
 * Created by xiasj on 18-1-2.
 */

public class BTController implements CallListener {
    private Context mContext;
    private IGocsdkService mGocsdkService;
    private IGocsdkCallback mGocsdkCallBack;
    private static CallListener sCallListener;

    @Override
    public void onCall(String number) {
        Intent intent = new Intent(mContext, CallActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    @Override
    public void onHangUp() {

    }

    @Override
    public void onTaking() {

    }

    @Override
    public void onCallIn(String number) {
        Intent intent = new Intent(mContext, InComingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    @Override
    public void onConnected() {

    }

    @Override
    public void onDisConnected() {

    }

    private static class LazyHolder {
        private static final BTController INSTANCE = new BTController();
    }

    private BTController() {
    }

    public static final BTController getInstance() {
        return LazyHolder.INSTANCE;
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            sCallListener = BTController.this;
            LOG.e("MainActivity-->onServiceConnected ");
            mGocsdkService = IGocsdkService.Stub.asInterface(iBinder);
            BTApplication.getInstance().setStatus(BTApplication.STATUS_INIT);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        mGocsdkCallBack = new GocsdkCallbackImp();
                        mGocsdkService.registerCallback(mGocsdkCallBack);
                        mGocsdkService.inqueryHfpStatus();
                        mGocsdkService.inqueryA2dpStatus();
                        mGocsdkService.musicUnmute();
                        mGocsdkService.getLocalName();
                        mGocsdkService.getCurrentDeviceName();
                    } catch (RemoteException e) {
                        LOG.e("MainActivity-->mGocsdkService RemoteException " + e.getMessage());
                    }
                }
            }, 500);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            sCallListener = null;
            LOG.e("MainActivity-->onServiceDisconnected ");
        }
    };

    public void init(Context context) {
        mContext = context.getApplicationContext();
        binderService();
    }

    public void release() {
        mContext.unbindService(mServiceConnection);
        mContext = null;
    }


    private void binderService() {
        Intent intent = new Intent(mContext, GocsdkService.class);
        if (isServiceWork("com.softwinner.awbt.service.GocsdkService")) {
            mContext.stopService(intent);
        }

        mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private boolean isServiceWork(String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) mContext.getSystemService(Context
                .ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(30);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }
        return isWork;
    }


    public void doScan() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.startDiscovery();
    }

    public void doRename(String name) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.setLocalName(name);
    }

    public void doConnect(String address) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.connectDevice(address);
    }

    public void setAutoConnect(boolean auto) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }

        if (auto) {
            mGocsdkService.setAutoConnect();
        } else {
            mGocsdkService.cancelAutoConnect();
        }
    }

    public void setAutoAnswer(boolean auto) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        if (auto) {
            mGocsdkService.setAutoAnswer();
        } else {
            mGocsdkService.cancelAutoAnswer();
        }
    }


    public void playOrPauseMusic() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.musicPlayOrPause();
    }

    public void toNextMusic() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.musicNext();
    }

    public void toPreviousMusic() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.musicPrevious();
    }

    public void getMusicInfo() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.getMusicInfo();
    }


    public void callPhone(String number) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.phoneDail(number);
    }

    /**
     * @param type 1: call out
     *             2: missed call
     *             3: call in
     */
    public void syncCallLogs(int type) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.callLogstartUpdate(type);
    }

    public void syncContacts() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }

        mGocsdkService.phoneBookStartUpdate();
    }

    public void phoneTransmitDTMFCode(char c) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }

        mGocsdkService.phoneTransmitDTMFCode(c);
    }

    public void phoneTransferBack() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }

        mGocsdkService.phoneTransferBack();
    }

    public void phoneTransfer() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }

        mGocsdkService.phoneTransfer();
    }

    public void phoneHangUp() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }

        mGocsdkService.phoneHangUp();
    }

    public void phoneAnswer() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }

        mGocsdkService.phoneAnswer();
    }

    public void disConnected() throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.disconnect();
    }

    public static final CallListener getCallListener() {
        return sCallListener;
    }

    public void setMicMute(boolean mute) throws RemoteException {
        if (mGocsdkService == null) {
            return;
        }
        mGocsdkService.micMute(mute);
    }

}
